package org.dromara.wms.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.common.web.core.BaseService;
import org.springframework.stereotype.Service;
import org.dromara.wms.domain.bo.WmsGroupBo;
import org.dromara.wms.domain.vo.WmsGroupVo;
import org.dromara.wms.domain.WmsGroup;
import org.dromara.wms.mapper.WmsGroupMapper;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 产品分组Service业务层处理
 *
 * @author Lion Li
 * @date 2024-03-20
 */
@RequiredArgsConstructor
@Service
public class WmsGroupServiceImpl extends BaseService {

    private final WmsGroupMapper baseMapper;

    /**
     * 查询产品分组
     */
    public WmsGroupVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询产品分组列表
     */
    public TableDataInfo<WmsGroupVo> queryPageList(WmsGroupBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WmsGroup> lqw = buildQueryWrapper(bo);
        Page<WmsGroupVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询产品分组列表
     */
    public List<WmsGroupVo> queryList(WmsGroupBo bo) {
        LambdaQueryWrapper<WmsGroup> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<WmsGroup> buildQueryWrapper(WmsGroupBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WmsGroup> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), WmsGroup::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getSpu()), WmsGroup::getSpu, bo.getSpu());
        lqw.between(params.get("beginUpdateTime") != null && params.get("endUpdateTime") != null,
            WmsGroup::getUpdateTime ,params.get("beginUpdateTime"), params.get("endUpdateTime"));
        return lqw;
    }

    /**
     * 新增产品分组
     */
    public Boolean insertByBo(WmsGroupBo bo) {
        WmsGroup add = MapstructUtils.convert(bo, WmsGroup.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改产品分组
     */
    public Boolean updateByBo(WmsGroupBo bo) {
        WmsGroup update = MapstructUtils.convert(bo, WmsGroup.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(WmsGroup entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除产品分组
     */
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
